package com.xkxx.task.service.impl;

import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.conditions.update.LambdaUpdateChainWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xkxx.task.entity.BatchData;
import com.xkxx.task.entity.BusinessData;
import com.xkxx.task.mapper.BatchDataMapper;
import com.xkxx.task.mapper.BusinessDataMapper;
import com.xkxx.task.service.BatchDataService;
import com.xkxx.task.service.BusinessDataService;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * @Author: K-9999
 * @Date: 2022/12/15
 * @Description:
 */
@Service
public class BatchDataServiceImpl extends ServiceImpl<BatchDataMapper, BatchData> implements BatchDataService {

    @Override
    public BatchData getBatchData(Integer appId, Integer batchNo) {
        return new LambdaQueryChainWrapper<>(getBaseMapper())
                .eq(BatchData :: getAppId, appId)
                .eq(BatchData :: getBatchNo, batchNo)
                .last("limit 1")
                .one();
    }

    @Override
    public Boolean updateBatchData(Integer id, BatchData data) {
        return new LambdaUpdateChainWrapper<>(getBaseMapper())
                .eq(BatchData :: getId,  id)
                .set(BatchData :: getStatus, 0)
                .set(BatchData :: getUpdateTime, new Date())
                .update(data);
    }

    @Override
    public Boolean updateBatchDataInProgress(Integer id, BatchData data) {
        return new LambdaUpdateChainWrapper<>(getBaseMapper())
                .eq(BatchData :: getId,  id)
                .set(BatchData :: getStatus, 1)
                .set(BatchData :: getUpdateTime, new Date())
                .update(data);
    }

    @Override
    public Boolean updateBatchDataDone(Integer id, BatchData data) {
        return new LambdaUpdateChainWrapper<>(getBaseMapper())
                .eq(BatchData :: getId,  id)
                .set(BatchData :: getStatus, 2)
                .set(BatchData :: getUpdateTime, new Date())
                .update(data);
    }

    @Override
    public Boolean addBatchData(BatchData data) {
        data.setStatus(0);
        data.setCreateTime(new Date());
        return this.save(data);
    }

    @Override
    public BatchData getBatchDataOrderByCreateTime() {
        return new LambdaQueryChainWrapper<>(getBaseMapper())
                .eq(BatchData :: getStatus, 0)
                .orderByAsc(BatchData :: getCreateTime)
                .last("limit 1")
                .one();
    }
}
